﻿using Microsoft.Data.SqlClient;
using HRMSystemShi.BLL;
using static HRMSystemShi.BLL.LoginUser;

namespace HRMSystemShi._2025
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            string un = textBoxUserName.Text.Trim();
            string pwd = CommonHelper.GetMD5(textBoxPassword.Text.Trim());

            LoginUser lu = new LoginUser(un, pwd);
            LoginType type = lu.Login();
            if (type == LoginType.Fail)
            {
                CommonHelper.MessageError("用户名或密码错误");
                this.DialogResult = DialogResult.Cancel;
            }
            else if (type == LoginType.Locked)
            {
                CommonHelper.MessageError("该用户已锁定");
                this.DialogResult = DialogResult.Cancel;
            }
            else if (type == LoginType.Deleted)
            {
                CommonHelper.MessageError("该用户已删除");
                this.DialogResult = DialogResult.Cancel;
            }
            else if (type == LoginType.Admin)
            {
                CommonHelper.MessageSuccess("管理员欢迎使用本系统");
                this.DialogResult = DialogResult.OK;
            }
            else if (type == LoginType.Login)
            {
                CommonHelper.MessageSuccess("欢迎使用本系统");
                this.DialogResult = DialogResult.OK;
            }

            /*string connstr = "Server=(localdb)\\MSSQLLocalDB; Database=HRMDB; User Id = hrmtest; Password=hrmtest";
            SqlConnection conn = new SqlConnection(connstr);
            string sql = "SELECT COUNT(*) FROM Operator WHERE UserName =@UserName AND Password=@Password";
            SqlParameter spun = new SqlParameter("@UserName", un);
            SqlParameter sppwd = new SqlParameter("@Password", pwd);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(spun);
            cmd.Parameters.Add(sppwd);

            conn.Open();
            int n = (int)cmd.ExecuteScalar();
            conn.Close();

            if (n > 0)
            {
                CommonHelper.MessageSuccess("欢迎使用本系统!");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                CommonHelper.MessageError("用户名或密码错误！");
                this.DialogResult= DialogResult.Cancel;
            }*/
        }

        private void linkLabeNewUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form formRegister = new Register();
            formRegister.Show();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (CommonHelper.MessageYesOrNo("确定退出吗?") == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void labelPassword_Click(object sender, EventArgs e)
        {

        }
    }
}
